package guestbook;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class GuestBookMain extends JFrame {

	static  String driver = "oracle.jdbc.driver.OracleDriver";
	static  String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
	//static  String url = "jdbc:oracle:thin:@192.168.10.9:1521:XE";
	static  String id = "hr";
	static  String password = "hr";
	
	Connection con;
	
	Input input;
	Update update;
	Select select;
	
	JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnMenu;
	private JMenuItem menuItem;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GuestBookMain frame = new GuestBookMain();
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // 윈도우 창처럼 UI변경
					SwingUtilities.updateComponentTreeUI(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GuestBookMain() {
		setTitle("방명록");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		// 데이터베이스 연결 테스트
				try {
					Class.forName(driver).newInstance();
					System.out.println("드라이버가 로딩되었습니다.");
					
					con=DriverManager.getConnection(url, "hr", "hr");
					System.out.println("데이터베이스가 정상적으로 연결됨");
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			openSelect();
	}
	
	public void openInput(){
		if(input == null){
			input = new Input(con,this);
			contentPane.add(input);
		}
		input.setVisible(true);
		input.toFront();
	}
	
	public void openUpdate(){
		if(update == null){
			update = new Update(con, this);
			contentPane.add(update);
		}
		update.setVisible(true);
		update.toFront();
	}
	
	public void openSelect(){
		if(select == null){
			select = new Select(con, this);
			contentPane.add(select);
		}
		select.setVisible(true);
		select.toFront();
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnMenu());
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMnMenu() {
		if (mnMenu == null) {
			mnMenu = new JMenu("menu");
			mnMenu.add(getMenuItem());
			mnMenu.add(getMntmNewMenuItem());
			mnMenu.add(getMntmNewMenuItem_1());
		}
		return mnMenu;
	}
	private JMenuItem getMenuItem() {
		if (menuItem == null) {
			menuItem = new JMenuItem("입력");
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openInput();
				}
			});
		}
		return menuItem;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("수정");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openUpdate();
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("조회");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openSelect();
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("Connect");
			mnNewMenu.add(getMntmNewMenuItem_2());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("서버연결");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String ip = JOptionPane.showInputDialog(null, "접속할 서버 주소를 입력하라~!");
					
					 url = "jdbc:oracle:thin:@"+ip+":1521:XE";
					 try{
						 con = DriverManager.getConnection(url, id, password);
						 InetAddress ia =InetAddress.getLocalHost();
						 setTitle("IP:"+ia.getHostAddress());
						 
						 
						 if(update !=null) select.setData();
					 }catch(Exception ex){
						 ex.printStackTrace();
					 }
					
				}
			});
		}
		return mntmNewMenuItem_2;
	}
}
